package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.entity.Inform
import cn.exrick.xboot.base.mapper.InformMapper
import cn.exrick.xboot.base.service.IInformService
import cn.exrick.xboot.core.common.utils.PageUtil
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.common.vo.PageVo
import cn.exrick.xboot.core.common.vo.Result
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.google.common.collect.Maps
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * 通知接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class IInformServiceImpl : ServiceImpl<InformMapper?, Inform?>(), IInformService {


    @Autowired
    private lateinit var securityUtil: SecurityUtil

    override fun getMyInfo(pageVo: PageVo): Result<MutableMap<String, Any?>>? {

        return ToolUtil.setListToNul(list(KtQueryWrapper(Inform::class.java).eq(Inform::informToUserId, securityUtil.currUser.id)
                .orderByDesc(Inform::createTime)))?.run {

            val result: MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, this)

            ResultUtil.data(result, "获取我的通知成功")
        } ?: ResultUtil.error(201, "暂无消息")
    }

    override fun getMyInfoDetail(id: String?): Result<Inform>? {

       return getById(id)?.run {

            isRead = true
            updateById(this)

            ResultUtil.data(this)
        } ?: ResultUtil.error(201, "不存在该消息")
    }

    override fun getMyInfoCount(): Result<Int> {

        return ResultUtil.data(count(KtQueryWrapper(Inform::class.java)
                .eq(Inform::isRead, false)
                .eq(Inform::createBy, securityUtil.currUser.id)))
    }

}